<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sell_books extends MIS_Controller {

	function __construct()
	{
		parent::__construct();
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/buy_books
	 *	- or -  
	 * 		http://example.com/buy-books
	 *	- or -  
	 * 		http://example.com/buy_books/index
	 *	- or -  
	 * 		http://example.com/buy-books/index
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if(!$this->is_loggedin) redirect('/registration');
		// Start Captcha Checking
		$this->loadRecaptcha();
		$config = array(
               array(
                     'field'   => 'subject', 
                     'label'   => 'Subject', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'book-name', 
                     'label'   => 'Book Name', 
                     'rules'   => 'trim|required|min_length[6]|max_length[50]'
                  ),
               array(
                     'field'   => 'phone', 
                     'label'   => 'phone', 
                     'rules'   => 'trim|required|numeric|min_length[10]|max_length[11]'
                  ), 
				array(
                     'field'   => 'university', 
                     'label'   => 'University', 
                     'rules'   => 'required'
                  ),
				array(
                     'field'   => 'campus', 
                     'label'   => 'Campus', 
                     'rules'   => 'required'
                  ),
				array(
                     'field'   => 'realPrice', 
                     'label'   => 'Real Price', 
                     'rules'   => 'trim|required|numeric'
                  ),
				array(
                     'field'   => 'yourPrice', 
                     'label'   => 'Your Price', 
                     'rules'   => 'trim|required|numeric|callback__isGreater'
                  ),
				
				array(
                     'field'   => 'iaccept', 
                     'label'   => 'Terms', 
                     'rules'   => 'trim|required'
                  ),
				array(
                     'field'   => 'isbn', 
                     'label'   => 'ISBN', 
                     'rules'   => 'required'
                  ),
				array(
                     'field'   => 'bookImage', 
                     'label'   => 'Book Photo', 
                     'rules'   => 'callback__file_upload_check'
                  ),
				array(
                     'field'   => 'description', 
                     'label'   => 'Book Description', 
                     'rules'   => 'required|trim'
                  )
            );
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules($config);
		if($this->form_validation->run())
		{
			$fileName=$this->session->userdata("filename");
			$data = array(
			   'uid' 		=> $this->session->userdata('id_user') ,
			   'Name' 		=> $this->input->post('book-name'),
			   'ISBN' 		=> $this->input->post('isbn'),
			   'Phone' 		=> $this->input->post('phone'),
			   'University' => $this->input->post('university'),
			   'Campus' 	=> $this->input->post('campus'),
			   'Description'=> $this->input->post('description'),
			   'Real_price' => $this->input->post('yourPrice'),
			   'Sell_price' => $this->input->post('realPrice'),
			   'Image' 		=> $fileName['file_name'],
			   'Subject'	=> $this->input->post('subject')
			);
			$this->loadViewData['recaptcha_html'] = "";
			$this->db->insert('book_bank', $data); 
			$this->session->unset_userdata('filename');
			$this->session->set_flashdata('success_mesg', 'Your book has been uploaded.');
			redirect("sell-books");
		} 
		else $this->loadViewData['recaptcha_html'] = $this->recaptcha->get_html();
		// End Captcha Checking
	
		$query = $this->db->get('subject');
		
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['search_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_sell_books';
		$this->loadViewData['subjectData'] = $query->result();
		
		$this->load->view('layout', $this->loadViewData);
	}
	function _file_upload_check()
	{
		if($_FILES['bookImage']['size']>0)
		{
			$fileName= 'image_'.md5(time());
			$config['upload_path'] = 'images/books/';
			$config['allowed_types'] = '*';
			$config['max_size']	= '1000';
			$fileName= 'image_'.md5(time());
			$config['file_name'] = $fileName;
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if ( ! $this->upload->do_upload("bookImage") )
			{
				$this->form_validation->set_message('_file_upload_check',$this->upload->display_errors('', ''));
				return FALSE;
			}
			else
			{
				$this->session->set_userdata('filename', $this->upload->data('file_name'));
				return TRUE;
			}
		}
		else return TRUE;
    } 
	function _isGreater()
	{
		$realPriceVal=$this->input->post('realPrice');
		if(!empty($realPriceVal))
		{
			if($this->input->post('realPrice')<$this->input->post('yourPrice'))
			{
				$this->form_validation->set_message('_isGreater','Your price must be less than Real Price.');
				return FALSE;
			}
			return TRUE;
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */